Conversation
|
Review requested:
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #66132 +/- ##
========================================
Coverage 90.27% 90.27%
========================================
Files 790 792 +2
Lines 271651 272380 +729
Branches 51842 52013 +171
========================================
+ Hits 245228 245889 +661
- Misses 16928 16946 +18
- Partials 9495 9545 +50
🚀 New features to boost your workflow:
|
This comment was marked as resolved.
This comment was marked as resolved.
Necessarily semver-major. When `--permission` is on, every env var not matched by `--allow-env` is removed at startup. It takes names, patterns (`PREFI_*`), or `*`, repeatable or comma-sep'd. There are a range of env vars that Node.js itself uses, and a default range that are generally known to be safe in common usage. These are never scrubbed. These include things like `NODE_OPTIONS`, `NODE_EXTRA_CA_CERTS`, `PATH`, `HOME`, etc. Env vars can be dropped at runtime after reading using `permission.drop()`. This is a stronger protection than using `process.env.FOO = undefined` because it will scrub the env var also from the environment block. On Linux, the removed entries are overwritten in the initial environment block and fs reads of /proc/*/environ are denied. On Windows, removal also clears the C runtime's copy of the environ using _wputenv_s Reading a removed name returns undefined, warns once per name, and publishes to a diagnostics channel. Env file keys are allowed. If the user had reason to pass in an env file the assumption is they meant to allow them. File-source config (node.config.json and NODE_OPTIONS from a .env file can only narrow the allow list. Embedders must call ScrubProcessEnvironment() themselves on startup. This is left up to the embedder to determine the exact timing but needs to be called before startup actually happens. Child processes are started with `--allow-env=*`. Those either receive the explicit env they were started with or only the env they inherit from the parent. Since the parent process is scrubbed, it should never be more than what the parent can see. Main part of the impl was done by hand. Docs, tests, verification pass, and cleanup nits were automated. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode
3740011 to
9127642
Compare
|
why semver-major? |
|
It's major because it's filter-by-default when |
RafaelGSS
left a comment
There was a problem hiding this comment.
We discussed this in nodejs/security-wg#993. I think we should revisit the concerns there before proceeding.
Removing the variables from the actual process environment addresses some of the issues with the previous approach, but I'm still not convinced about the use case. For instance, if my application needs AWS_SECRET_ACCESS_KEY and I allow it, every dependency can still read it. If the application doesn't need it, why not remove it before starting Node.js?
The read-then-drop case seems more useful, but does permission.drop() also remove the value from the initial environment block? From reading RemoveFromEnvironment, it seems that value would still be there on Linux.
I'd like to understand which use case requires this in core and what we intend to guarantee before getting into the implementation details, because as soon as we land this, I do expect a lot of AI-Sloop on H1... sadly.
Yes, it should. The idea is keep the vars for as long as needed then scrub them. |
Necessarily semver-major.
Alternative for a previous attempt in #62827 that stalled out.
When
--permissionis on, every env var not matched by--allow-envis removed at startup. It takes names, patterns (PREFI_*), or*, repeatable or comma-sep'd.There are a range of env vars that Node.js itself uses, and a default range that are generally known to be safe in common usage. These are never scrubbed. These include things like
NODE_OPTIONS,NODE_EXTRA_CA_CERTS,PATH,HOME, etc.Env vars can be dropped at runtime after reading using
permission.drop(). This is a stronger protection than usingprocess.env.FOO = undefinedbecause it will scrub the env var also from the environment block.On Linux, the removed entries are overwritten in the initial environment block and fs reads of /proc/*/environ are denied.
On Windows, removal also clears the C runtime's copy of the environ using _wputenv_s
Reading a removed name returns undefined, warns once per name, and publishes to a diagnostics channel.
Env file keys are allowed. If the user had reason to pass in an env file the assumption is they meant to allow them.
File-source config (node.config.json and NODE_OPTIONS from a .env file can only narrow the allow list.
Embedders must call ScrubProcessEnvironment() themselves on startup. This is left up to the embedder to determine the exact timing but needs to be called before startup actually happens.
Child processes are started with
--allow-env=*. Those either receive the explicit env they were started with or only the env they inherit from the parent. Since the parent process is scrubbed, it should never be more than what the parent can see.The key motivation here is that environment variables are the primary mechanism for injecting secrets into applications. These can be trivially exfiltrated using simple one-liners like
fetch('...', { headers: { secret: process.env.SECRET } }). Simply unsetting those viaprocess.env.SECRET = undefinedis typically not enough since those can still be read from the underlying environment block. This PR provides stronger protection but it obviously cannot be 100% since depending on how it is used, the strings can still be in memory.The prior attempt to add this in #62827 had a number of flaws that this version addresses. That PR used an incomplete view filter.
process.report.getReport(), native addons, FFI,and
/proc/self/environcould all still read the real environment. Scrubbingbefore any JavaScript runs protects those paths. This PR also fixes a number of other breaking changes the other PR would have introduced:
process.envandprocess.loadEnvFile()keep working.--env-filefiles are allowed.--allow-env=*, because the environment they inherit has already been scrubbed.